layui.use(['table', 'admin', 'ax', 'func', 'layer'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;
    var layer = layui.layer;

    $("#returnBefore").click(function () {
        history.go(-1)
    })

    /**
     * 获取上一个页面的metadataId
     */
    var metadataId = Feng.getUrlParam("metadataId")
    // console.log(metadataId)

    /**
     * 元数据详情表管理
     */
    var MetadataDetail = {
        tableId: "metadataDetailTable"
    };

    /**
     * 初始化表格的列
     */
    MetadataDetail.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'metadataDetailId', hide: true, title: '元数据详情id'},
            {field: 'metadataId', sort: true, title: '关联metadata表', hide: true},
            {field: 'metadataDetailUsName', sort: false, title: '字段英文名', align: "center"},
            {field: 'metadataDetailCnName', sort: false, title: '字段中文名', align: "center"},
            {field: 'metadataDetailDataType', sort: false, title: '数据类型', align: "center"},
            {field: 'metadataDetailDataLength', sort: false, title: '数据长度', align: "center"},
            {
                field: 'metadataDetailIsRequired',
                sort: false,
                title: '是否必填',
                align: "center",
                templet: function (d) {
                    if (d.metadataDetailIsRequired == 1) {
                        return "是"
                    } else if (d.metadataDetailIsRequired == 2) {
                        return "否"
                    }
                }
            },
            {field: 'metadataDetailFormat', sort: false, title: '填写格式', align: "center"},
            {field: 'isDelete', sort: true, title: '是否删除;0代表删除；1代表可用', hide: true},
            {align: 'center', toolbar: '#tableBar', title: '操作', hide: true}
        ]];
    };

    /**
     * 点击查询按钮
     */
    MetadataDetail.search = function () {
        var queryData = {};

        queryData['metadataDetailId'] = $('#metadataDetailId').val();
        queryData['metadataId'] = $('#metadataId').val();
        queryData['metadataDetailUsName'] = $('#metadataDetailUsName').val();
        queryData['metadataDetailCnName'] = $('#metadataDetailCnName').val();
        queryData['metadataDetailDataType'] = $('#metadataDetailDataType').val();
        queryData['metadataDetailDataLength'] = $('#metadataDetailDataLength').val();
        queryData['metadataDetailIsRequired'] = $('#metadataDetailIsRequired').val();
        queryData['metadataDetailFormat'] = $('#metadataDetailFormat').val();
        queryData['isDelete'] = $('#isDelete').val();

        table.reload(MetadataDetail.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 弹出添加对话框
     */
    MetadataDetail.openAddDlg = function () {
        layer.open({
            title: '添加元数据详情表',
            type: 2,
            area: ['80%', '80%'],
            content: Feng.ctxPath + '/metadataDetail/add?metadataId=' + metadataId,
            tableId: MetadataDetail.tableId,
            end: function () {
                table.reload(MetadataDetail.tableId)
            }
        });
    };

    /**
     * 修改
     */
    $("#btnEdit").click(function () {
        if (ids.length != 1) {
            Feng.error("请选择一条数据")
            return;
        }
        layer.open({
            title: '修改元数据详情表',
            content: Feng.ctxPath + '/metadataDetail/edit?metadataDetailId=' + ids[0],
            tableId: MetadataDetail.tableId,
            type: 2,
            area: ['80%', '80%'],
            end: function () {
                table.reload(MetadataDetail.tableId);
                ids = []
            }
        });
    })


    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    // MetadataDetail.openEditDlg = function (data) {
    //     layer.open({
    //         title: '修改元数据详情表',
    //         type: 2,
    //         area: ['80%', '80%'],
    //         content: Feng.ctxPath + '/metadataDetail/edit?metadataDetailId=' + data.metadataDetailId,
    //         tableId: MetadataDetail.tableId,
    //         end: function () {
    //             table.reload(MetadataDetail.tableId)
    //         }
    //     });
    // };


    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    // MetadataDetail.onDeleteItem = function (data) {
    //     var operation = function () {
    //         var ajax = new $ax(Feng.ctxPath + "/metadataDetail/delete", function (data) {
    //             Feng.success("删除成功!");
    //             table.reload(MetadataDetail.tableId);
    //         }, function (data) {
    //             Feng.error("删除失败!" + data.responseJSON.message + "!");
    //         });
    //         ajax.set("metadataDetailId", data.metadataDetailId);
    //         ajax.start();
    //     };
    //     Feng.confirm("是否删除?", operation);
    // };

    /**
     * 数据删除
     */
    $("#btnDelete").click(function () {
        if (ids.length == 0) {
            Feng.error("请至少选择一条数据")
            return;
        }
        if (ids.length == 1) {
            var operation = function () {
                $.ajax(Feng.ctxPath + '/metadataDetail/deleteMetadataDetail', {
                    data: {
                        arr: ids
                    },
                    //开启序列化
                    traditional: true,
                    success: function (res) {
                        // console.log(res)
                        if (res == ids.length) {
                            Feng.success('删除成功')
                            table.reload(MetadataDetail.tableId);
                        }
                    },
                    error: function () {
                        Feng.error('删除失败')
                    }
                })
            };
            Feng.confirm("是否删除这条数据?", operation);
        }
        if (ids.length > 1) {
            var operation = function () {
                $.ajax(Feng.ctxPath + '/metadataDetail/deleteMetadataDetail', {
                    data: {
                        arr: ids
                    },
                    //开启序列化
                    traditional: true,
                    success: function (res) {
                        // console.log(res)
                        if (res == ids.length) {
                            Feng.success('删除成功')
                            table.reload(MetadataDetail.tableId);
                        }
                    },
                    error: function () {
                        Feng.error('删除失败')
                    }
                })
            };
            Feng.confirm("是否删除这" + ids.length + "条数据?", operation);
        }
    })


    // 渲染表格
    var tableResult = table.render({
        elem: '#' + MetadataDetail.tableId,
        url: Feng.ctxPath + '/metadataDetail/list?metadataId=' + metadataId,
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        skin: "line",
        cols: MetadataDetail.initColumn(),
        done: function (res, curr, count) {
            $('tr').css('background-color', '#ffffff')
            table_data = res.data;
        }
    });

    /**
     * 获取表格复选框内的值
     */
    var table_data = new Array();
    var ids = new Array();
    table.on('checkbox(' + MetadataDetail.tableId + ')', function (obj) {
        // console.log(obj)
        if (obj.checked == true) {
            if (obj.type == 'one') {
                ids.push(obj.data.metadataDetailId);
            } else {
                for (var i = 0; i < table_data.length; i++) {
                    ids.push(table_data[i].metadataDetailId);
                }
            }
        } else {
            if (obj.type == 'one') {
                for (var i = 0; i < ids.length; i++) {
                    if (ids[i] == obj.data.metadataDetailId) {
                        ids.splice(i, 1);
                    }
                }
            } else {
                for (var i = 0; i < ids.length; i++) {
                    for (var j = 0; j < table_data.length; j++) {
                        if (ids[i] == table_data[j].metadataDetailId) {
                            ids.splice(i, 1);
                        }
                    }
                }
            }
        }
    });


    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        MetadataDetail.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {

        MetadataDetail.openAddDlg();

    });

    // 导出excel
    $('#btnExp').click(function () {
        MetadataDetail.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + MetadataDetail.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            MetadataDetail.openEditDlg(data);
        } else if (layEvent === 'delete') {
            MetadataDetail.onDeleteItem(data);
        }
    });
});
